package testEsClient;

import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.nio.charset.Charset;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.LongSerializationPolicy;
import com.google.gson.reflect.TypeToken;

public class test {

	/**
	 * 打印 tz对应的日期/时间
	 */
	private static void printDateIn(TimeZone tz) {
		// date为2013-09-19 14:22:30
		Date date = new Date(113, 8, 19, 14, 22, 30);
		// 获取默认的DateFormat，用于格式化Date
		DateFormat df = DateFormat.getInstance();
		// 设置时区为tz
		df.setTimeZone(tz);
		// 获取格式化后的字符串
		String str = df.format(date);

		System.out.println(tz.getID() + " :" + str);
	}


	public static String parseString(Object str){
        String returnValue = "";
        try {
            returnValue = new BigDecimal(String.valueOf(str)).toString();
            if(StringUtils.endsWith(returnValue, ".0")) {
                returnValue = StringUtils.removeEnd(returnValue, ".0");
            }
        } catch (Exception e) {
            returnValue = ObjectUtils.toString(str);
        }
        return returnValue;
    }
	
	public static void test2() {
		Gson gson = new Gson();
		String body = "{\"mount_source_id_str\":\"null\",\"biflag\":\"null\",\"is_js\":0,\"source_id_str\":\"null\",\"current_short_url\":\"detail/44299898\",\"agm\":\"phone\",\"referrer_pos\":\"null\",\"screen\":\"360x640\",\"agn\":\"chrome\",\"device_type\":\"mobile\",\"link_click_coordinate\":\"null\",\"agt\":\"chrome\",\"pre_pathid\":\"null\",\"agv\":\"62.0.3202.84\",\"companyid\":3037782471,\"protocol\":\"http:\",\"house_geo\":[113.87796687872,22.929232137828],\"mount_referrer_domain\":\"null\",\"referrer_short_url\":\"info/514-3/l1-k2\",\"price\":145.29,\"ctime\":\"2017-11-30T08:16:59\",\"click_coordinate\":\"256\u00261102^\",\"block\":859,\"isquality\":1,\"current_path\":\"detail\",\"isbot\":\"0\",\"agentid\":3194451,\"mount_keyword_type\":\"null\",\"houseid\":\"44299898\",\"ip\":\"219.79.117.115\",\"mount_keyword\":\"null\",\"rnd\":\"264931328\",\"current_domain\":\"m.leju.com\",\"mount_promo_type\":\"null\",\"buildingarea\":75.63,\"pubtime\":1504051823,\"is_new_user\":\"0\",\"mount_ktype\":\"null\",\"district\":858,\"current_domain_int\":\"1\",\"referrer_domain_int\":\"1\",\"shopid\":5147756541,\"is_pay\":0,\"status\":2,\"ktype\":\"null\",\"city\":\"dg\",\"propertype\":1,\"useragent\":\"mozilla/5.0 (linux; android 7.0; eva-l29 build/huaweieva-l29) applewebkit/537.36 (khtml\u0026 like gecko) chrome/62.0.3202.84 mobile safari/537.36\",\"current_is_esf\":\"1\",\"shoptype\":0,\"baidu_y\":22.929232137828,\"baidu_x\":113.87796687872,\"referrer_next_path\":\"514-3\",\"referrer_path\":\"info\",\"referrer_domain\":\"m.leju.com\",\"is_rec\":99,\"last_time\":\"1512001015292\",\"gatheruuid\":\"215w09y901760366\",\"mount_promo_channel\":\"null\",\"is_refresh_user\":\"0\",\"sinaid\":514,\"floor\":15,\"colordepth\":\"32\",\"outside_keyword\":\"null\",\"phpsessid\":\"cd61e01ffcce3c65e3683cc460695d5a\",\"current_next_path\":\"44299898\",\"web_type\":\"2\",\"mount_source_id\":\"0\",\"room\":3,\"tradetype\":1,\"downtime\":1504006214,\"current_url\":\"http://m.leju.com/touch/esf/dg/detail/44299898/\",\"referrer_is_esf\":\"1\",\"current_path_id\":\"44299898\",\"promo_type\":\"null\",\"refresh_num\":\"0\",\"promo_channel\":\"null\",\"promo_pos\":\"null\",\"staytime\":\"6071\",\"current_other_path\":\"null\",\"step\":\"22\",\"referrer_other_path\":\"l1-k2\",\"source_id\":\"0\",\"mount_promo_pos\":\"null\",\"lejuglobal\":\"695769def3ef0b0c8d11cb069ba4c4bc\",\"referrer_url\":\"http://m.leju.com/touch/esf/dg/info/514-3/l1-k2/\"}";
		
		Type type = new TypeToken<Map<String, Object>>() {}.getType();
		Map<String, Object> jsonHash = gson.fromJson(new String(body.getBytes(), Charset.defaultCharset()), type);
		
		// 这只版本号
		System.out.println("jsonHash");
		System.out.println(jsonHash);
		for (String key : jsonHash.keySet()) {
			System.out.println(key + ":" + jsonHash.get(key).getClass());
		}
		
		
		Gson gson2 = new GsonBuilder()
		.setLongSerializationPolicy(LongSerializationPolicy.STRING)
		.serializeSpecialFloatingPointValues()
		.create();
		Map<String, Object> jsonHash2 = gson2.fromJson(new String(body.getBytes(), Charset.defaultCharset()), type);
		
		
		// 这只版本号
		System.out.println("jsonHash2");
		System.out.println(jsonHash2);
		for (String key : jsonHash2.keySet()) {
			System.out.println(key + ":" + jsonHash.get(key).getClass() + ":" + parseString(jsonHash.get(key)));
		}
		
		
		Gson gson3 = new GsonBuilder()
		.create();
		Map<String, Object> jsonHash3 = gson3.fromJson(new String(body.getBytes(), Charset.defaultCharset()), type);
		
		// 这只版本号
		System.out.println("jsonHash3");
		System.out.println(jsonHash3);
		for (String key : jsonHash3.keySet()) {
			System.out.println(key + ":" + jsonHash.get(key).getClass());
		}
		
		System.exit(0);
	}
	public static void test3() {
		Double d = 0.0;
		Long l = 0L;
		Integer i = 0;
		System.out.println(d.toString());
		System.out.println(d.toString().equals("0.0"));
		System.out.println(d == 0);
		System.out.println(l.toString());
		System.out.println(l.toString().equals("0"));
		System.out.println(l == 0);
		System.out.println(i.toString());
		System.out.println(i.toString().equals("0"));
		System.out.println(i == 0);
		System.exit(0);
	}
	public static void test4() {
//		ArrayList
//		class:class java.util.ArrayList
//		isArray:false
//		Else-2:house_geo:[113.30255471548, 22.476678259461]
		ArrayList house_geo = new ArrayList(){};
		house_geo.add(113.30255471548);
		house_geo.add(22.476678259461);
		
		System.out.println("isArray1:" + (house_geo instanceof java.util.ArrayList));
		System.out.println("isArray2:" + house_geo.getClass().isArray());
		System.exit(0);
	}
	public static void main(String[] args) throws Exception {
//		test4();
//		test3();
//		test2();

		TimeZone tz;

		// (01) 默认时区
		tz = TimeZone.getDefault();
		printDateIn(tz);

		// (02) 设置时区为"GMT+08:00"
		tz = TimeZone.getTimeZone("Etc/UTC");
		printDateIn(tz);

		// (02) 设置时区为"GMT+08:00"
		tz = TimeZone.getTimeZone("GMT+08:00");
		printDateIn(tz);

		// (03) 设置时区为""
		tz = TimeZone.getTimeZone("Asia/Shanghai");
		printDateIn(tz);

	}
}
